package tec.line;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLpara {
	
	
	public List<IndexData> para() throws ParserConfigurationException, SAXException, IOException{
		
		
		InputStream input = this.getClass().getClassLoader().getResourceAsStream("data.xml"); 
		
		List<IndexData> list = new ArrayList<IndexData>();  
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
        DocumentBuilder builder = factory.newDocumentBuilder();  
        Document document = builder.parse(input);  
        Element element = document.getDocumentElement();  
          
        NodeList IndexDataNodes = element.getElementsByTagName("data");  
        for(int i=0;i<IndexDataNodes.getLength();i++){  
            Element IndexDataElement = (Element) IndexDataNodes.item(i);  
            IndexData IndexData = new IndexData();  
//            IndexData.setId(Integer.parseInt(IndexDataElement.getAttribute("id")));  
            NodeList childNodes = IndexDataElement.getChildNodes();  
            for(int j=0;j<childNodes.getLength();j++){
                if(childNodes.item(j).getNodeType()==Node.ELEMENT_NODE){  
                    if("no".equals(childNodes.item(j).getNodeName())){  
                        IndexData.setId(Integer.parseInt(childNodes.item(j).getFirstChild().getNodeValue()));  
                    }else if("indexmax".equals(childNodes.item(j).getNodeName())){  
                        IndexData.setIndex_max(Integer.parseInt(childNodes.item(j).getFirstChild().getNodeValue()));
                    }else if("indexmin".equals(childNodes.item(j).getNodeName())){  
                    	IndexData.setIndex_min(Integer.parseInt(childNodes.item(j).getFirstChild().getNodeValue())); 
                    }else if("indexopen".equals(childNodes.item(j).getNodeName())){  
                    	IndexData.setIndex_open(Integer.parseInt(childNodes.item(j).getFirstChild().getNodeValue()));
                    }else if("indexclose".equals(childNodes.item(j).getNodeName())){  
                    	IndexData.setIndex_close(Integer.parseInt(childNodes.item(j).getFirstChild().getNodeValue()));
                }  
            }
            }
            list.add(IndexData);  
        }
        return list;
	}
	public static void main(String[] args){
		XMLpara xml = new XMLpara();
		
		try {
			System.out.println(xml.para());
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
}
